using Godot;

namespace GoGameClient.script.dialog;

public static class TipsDialogUtil
{
    private static BoxContainer _boxContainer;

    public static void ShowTips(Node parent, string text)
    {
        _boxContainer = parent.GetNode<BoxContainer>("/root/TipsDialog/TipsDialog");
        var ninePatchRect = CreatePatchRect(text);
        _boxContainer.AddChild(ninePatchRect);
        AddTimer(ninePatchRect);
    }

    public static void AddTimer(Node node)
    {
        var timer = new Timer();
        node.AddChild(timer); // 将定时器添加到当前节点
        // 设置定时器的时间间隔（以秒为单位）
        timer.WaitTime = 2;
        // 将定时器连接到一个方法，这个方法会在定时器到期时被调用
        timer.Timeout += OnTimerTimeout;
        // 启动定时器
        timer.Start();
    }

    public static void OnTimerTimeout()
    {
        var ninePatchRect = _boxContainer.GetChild<NinePatchRect>(_boxContainer.GetChildCount() - 1);
        _boxContainer.RemoveChild(ninePatchRect);
        ninePatchRect.QueueFree();
        if (_boxContainer.GetChildCount() <= 0)
        {
            _boxContainer = null;
        }
    }

    private static NinePatchRect CreatePatchRect(string text)
    {
        var patchRect = new NinePatchRect();
        patchRect.PatchMarginBottom = 14;
        patchRect.PatchMarginTop = 14;
        patchRect.PatchMarginLeft = 15;
        patchRect.PatchMarginRight = 15;
        patchRect.Texture = GD.Load<Texture2D>("res://resources/Ui/common/common-tips-backbround.png");
        var label = new Label();
        label.Text = text;
        label.Position = new Vector2(8, 0);
        label.VerticalAlignment = VerticalAlignment.Center;
        label.SetAnchorsPreset(Control.LayoutPreset.FullRect);
        label.LabelSettings = GD.Load<LabelSettings>("res://resources/setting/tips_settings.tres");
        patchRect.AddChild(label);
        return patchRect;
    }
}